package com.bishe.common.controller;

import cn.hutool.core.lang.Validator;
import com.bishe.common.utils.MailMsg;
import com.bishe.common.utils.Result;
import jakarta.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/mail")
@RequiredArgsConstructor
public class MailController {
    private  final MailMsg   mailMsg;
    private final RabbitTemplate rabbitTemplate;
    @PostMapping
    public Result sendMail(@RequestParam("email") String email)  {
        if (!Validator.isEmail(email)) {
            return Result.error("邮箱格式不正确");
        }
        try {
            rabbitTemplate.convertAndSend("mail.topic", "mail.success", email);
        }
        catch (Exception e){
            return Result.error("发送失败");
        }
        return Result.success("发送成功");
    }
}
